/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servinf.ani.sincronizador.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author mortiz
 */
public class LogHelper {
    
    public static final String DEBUG = "DEBUG";
    public static final String INFO = "INFO";
    public static final String WARNING = "WARNING";
    public static final String ERROR = "ERROR";
    
    private static FileWriter fileWritter;
    private static File logFile;
    private static File errorLogFile;
    
    public static void Log(String level, String message, Exception e) throws IOException {
        fileWritter = new FileWriter(logFile.getAbsolutePath(),true);   
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        String dateString = dateFormat.format(cal.getTime()); //2014/08/06 16:00:22
        bufferWritter.newLine();
        bufferWritter.write(dateString + " " + level + " :  " + message);
        if(level.equals(ERROR) && e != null) {
            bufferWritter.newLine();
            if(e.getMessage() != null) {
                bufferWritter.write(e.getMessage());
                FileWriter errorFileWritter = new FileWriter(errorLogFile.getAbsolutePath(),true);   
                BufferedWriter errorbufferWritter = new BufferedWriter(errorFileWritter);
                errorbufferWritter.newLine();
                errorbufferWritter.write(dateString + " " + level + " :  " + message);
                errorbufferWritter.close();
            }
            
            /* Para imprimir la traza completa del error.
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            sw.toString(); // stack trace as a string
            */
           
        }
        bufferWritter.close();
    }
    
    static public void setup() throws IOException {
        //Se obtiene la ruta de la carpeta de logs
        String logsFolder = PropertiesHelper.getInstance().getProperties().getProperty("log.filePath");
        File folder =new File(logsFolder);
        //if file doesnt exists, then create it
        if(!folder.exists()){
                folder.mkdirs();
        }
        String logName = folder.getAbsolutePath().trim() + "\\SincronizadorANI-" + now() + ".txt";
        String errorLogName = folder.getAbsolutePath().trim() + "\\SincronizadorANI-ERROR-" + now() + ".txt";
        File log = new File(logName);
        File errorLog = new File(errorLogName);
        if(!errorLog.exists()) {
            errorLog.createNewFile();
        }
        if(!log.exists()) {
            log.createNewFile();
        }
        logFile = log;
        errorLogFile = errorLog;
    }
    
    
    private static String now() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH-mm-yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }
    
}
